package com.internetCafes.spms.web.certificate.bizservice.impl;

import cn.hutool.core.bean.BeanUtil;
import com.internetCafes.spms.web.certificate.bizservice.IEmployeeBizService;
import com.internetCafes.spms.web.certificate.model.employee.req.EmployeeReq;
import com.internetCafes.spms.web.certificate.model.employee.res.EmployeeRes;
import com.internetCafes.spms.web.certificate.service.EmployeeInfoService;
import com.internetCafes.spms.web.tenant.common.TenantConst;
import com.internetCafes.spms.web.tenant.model.res.TenantDeptInfoRes;
import com.internetCafes.spms.web.tenant.service.TenantDeptInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class EmployeeBizServiceImpl implements IEmployeeBizService {

    @Autowired
    EmployeeInfoService employeeInfoService;

    @Autowired
    TenantDeptInfoService tenantDeptInfoService;

    @Override
    public List<EmployeeRes> list(EmployeeReq req) {
        return employeeInfoService.list(req)
                .stream()
                .map(employeePo -> BeanUtil.toBean(employeePo, EmployeeRes.class))
                .collect(Collectors.toList());
    }


    @Override
    public List<TenantDeptInfoRes> tree() {
        // 获取所有成员
        List<EmployeeRes> employeeResList = list(new EmployeeReq());

        List<Long> deptIdList = employeeResList.stream()
                .map(EmployeeRes::getDeptId)
                .distinct()
                .collect(Collectors.toList());

        // 获取所有部门并把成员装进响应的部门
        List<TenantDeptInfoRes> tenantDeptInfoResList = tenantDeptInfoService.queryByIds(deptIdList).stream()
                .map(tenantDeptInfo -> {
                    ArrayList<EmployeeRes> memberList = new ArrayList<>();
                    for (EmployeeRes member : employeeResList) {
                        if (tenantDeptInfo.getId().equals(member.getDeptId())) {
                            memberList.add(member);
                        }
                    }
                    TenantDeptInfoRes tenantDeptInfoRes = BeanUtil.toBean(tenantDeptInfo, TenantDeptInfoRes.class);
                    tenantDeptInfoRes.setMemberList(memberList);
                    return tenantDeptInfoRes;
                }).collect(Collectors.toList());

        // 获取根数据列表
        List<TenantDeptInfoRes> resList = tenantDeptInfoResList
                .stream()
                .filter(po -> TenantConst.TENANT_DEPT_ROOT_PARENT_ID.equals(po.getParentId()))
                .collect(Collectors.toList());

        // 对父节点进行分组
        Map<Long, List<TenantDeptInfoRes>> deptResGroupByParentId = tenantDeptInfoResList.stream()
                .collect(Collectors.groupingBy(TenantDeptInfoRes::getParentId));

        tree(resList, deptResGroupByParentId);
        return resList;
    }

    private void tree(List<TenantDeptInfoRes> nodeList, Map<Long, List<TenantDeptInfoRes>> deptResGroupByParentId) {
        List<TenantDeptInfoRes> children;
        for (TenantDeptInfoRes deptRes : nodeList) {
            if (deptResGroupByParentId.containsKey(deptRes.getId())) {
                children = deptResGroupByParentId.get(deptRes.getId());
                tree(children, deptResGroupByParentId);
                deptRes.setChildren(deptResGroupByParentId.get(deptRes.getId()));
            }
        }
    }

}
